<template>
  <div>
    <el-card>
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">设备管理</el-breadcrumb-item>
          <el-breadcrumb-item>历史排查记录</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <!--1.多条件查询栏 -->
      <el-row>
        <el-col>
          <el-form
            :inline="true"
            :model="recordSearch"
            class="demo-form-inline"
            style="margin-top: 10px"
          >
            <el-form-item label="开始时间">
              <el-date-picker
                v-model="recordSearch.starttime"
                align="right"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间">
              <el-date-picker
                v-model="recordSearch.endtime"
                align="right"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button
                icon="el-icon-search"
                type="primary"
                circle
                @click="getByCondition()"
              ></el-button>
              <el-button type="primary" @click="clearSearchForm()"
                >清空</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-row style="margin-top: 40px; margin-bottom: 10px">
        <el-col :span="6" :offset="0">
          <el-button type="danger" @click="batchDeleteClick()"
            >批量删除</el-button
          >
        </el-col>
      </el-row>
      <!--3.用户表单 -->
      <el-row>
        <el-col>
          <el-table
            :data="deviceCheckList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            @expand-change="handleExpandChange"
          >
            <el-table-column type="selection" width="55"> </el-table-column>
            <el-table-column type="expand">
              <template slot-scope="props">
                <!-- 这里放置扩展后显示的内容 -->
                <el-table
                  :data="props.row.itemList"
                  style="margin-left: 200px"
                  class="demo-table-expand"
                >
                  <el-table-column label="设备位置" width="150">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{
                        scope.row.device.cell.cname
                      }}</span>
                    </template>
                  </el-table-column>

                  <el-table-column label="设备名称" width="100">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{
                        scope.row.device.dname
                      }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="设备编号" width="100" align="center">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{
                        scope.row.device.dnumber
                      }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="责任人" width="200" align="center">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px"
                        >{{ scope.row.device.principal.uname }}（{{
                          scope.row.device.principal.phone
                        }}）</span
                      >
                    </template>
                  </el-table-column>

                  <!-- <el-table-column label="损毁程度" width="100" align="center">
                    <template slot-scope="scope">
                      <el-tag>
                        {{ scope.row.damagedegree }}
                      </el-tag>
                    </template>
                  </el-table-column> -->

                  <el-table-column label="损毁程度" width="100" align="center">
                    <template slot-scope="scope">
                      <div style="margin-left: 10px" class="tag5">
                        {{ getDamageDegree(scope.row.damagedegree) }}
                      </div>
                    </template>
                  </el-table-column>

                  <el-table-column label="排查时间" width="200" align="center">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{
                        scope.row.checktime
                      }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="备注" width="100" align="center">
                    <template slot-scope="scope">
                      <span style="margin-left: 10px">{{
                        scope.row.mark
                      }}</span>
                    </template>
                  </el-table-column>
                </el-table>
                <p>{{ props.row.description }}</p>
              </template>
            </el-table-column>
            <el-table-column label="编号" width="70">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.dcid }}</span>
              </template>
            </el-table-column>

            <el-table-column label="开始时间" width="200">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.starttime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="结束时间" width="200" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{ scope.row.endtime }}</span>
              </template>
            </el-table-column>
            <el-table-column label="排查设备数量" width="150" align="center">
              <template slot-scope="scope">
                <span style="margin-left: 10px">{{
                  scope.row.checknumber
                }}</span>
              </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="500">
              <template slot-scope="scope">
                <el-button
                  plain
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <!--4.分页 -->
      <el-row style="margin-top: 40px">
        <el-col>
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[2, 3, 4, 5]"
            :page-size="size"
            layout="prev, pager, next, jumper,total, sizes"
            :total="total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //1.查询
      recordSearch: {},
      //2.表格
      multipleSelection: [],
      deviceCheckList: [],
      //3.分页
      page: 1,
      size: 3,
      total: 0,
      isPageChangeToOne: true,
    };
  },
  methods: {
    //------------------------查询表单
    //多条件查询
    getByCondition() {
      this.$axios
        .post(
          "/deviceCheck/getByCondition/" + this.page + "/" + this.size,
          this.recordSearch
        )
        .then((respData) => {
          this.deviceCheckList = respData.data.data.beanList;
          this.page = respData.data.data.pageIndex;
          this.total = respData.data.data.totalRecord;
          for (const deviceCheck of this.deviceCheckList) {
            this.$axios
              .get("/deviceCheckItem/getByDcid/" + deviceCheck.dcid)
              .then((resp) => {
                deviceCheck.itemList = resp.data.data;
              });
          }
          console.log(this.deviceCheckList);
        });
    },
    //清空多条件查询表单
    clearSearchForm() {
      this.recordSearch = {};
      this.getByCondition();
    },
    // -----------------------表格
    //批量删除按钮
    batchDeleteClick() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请至少选择一条数据！");
      } else {
        this.$confirm("确定删除这些数据吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let deleteParam = "?";
            for (var index in this.multipleSelection) {
              deleteParam +=
                "dcids=" + this.multipleSelection[index].dcid + "&";
            }
            this.$axios
              .delete("/deviceCheck/batchedDelete" + deleteParam)
              .then((respData) => {
                if (respData.data.data) {
                  this.$message.success("批量删除成功！");
                  this.getByCondition();
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },

    //表格多选框事件
    handleSelectionChange(rows) {
      this.multipleSelection = rows;
      console.log(this.multipleSelection);
    },
    handleExpandChange(row, expandedRows) {
      console.log(row);
      console.log(expandedRows);
    },
    //表格删除事件
    handleDelete(index, row) {
      this.$confirm("确定删除这条信息吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete("/deviceCheck/deleteById/" + row.dcid)
            .then((respData) => {
              console.log(respData);
              if (respData.data.data) {
                this.$message.success("删除成功！");
                this.getByCondition();
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //表格损毁程度
    getDamageDegree(status) {
      switch (status) {
        case 0:
          return "正常";
        case 1:
          return "一级损毁";
        case 2:
          return "二级损毁";
        case 3:
          return "三级损毁";
      }
    },

    //--------------------------分页
    //分页尺寸变化
    handleSizeChange(size) {
      this.size = size;
      this.getByCondition();
    },
    //分页页码变化
    handleCurrentChange(page) {
      this.isPageChangeToOne = false;
      this.page = page;
      this.getByCondition();
      this.isPageChangeToOne = true;
    },
  },
  mounted() {
    this.getByCondition();
  },
};
</script>

<style>
.demo-table-expand {
  font-size: 7;
  color: #409eff;
}
.demo-table-expand label {
  width: 90px;
}

.demo-table-expand .el-table-column {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.demo-table-expand .el-table-column label {
  width: 90px;
  color: #409eff;
}
.tag5 {
  text-align: center;
  border-radius: 12px;
  background: #e8f8f3;
  border: 1px solid #f3b509;
  color: #f3b509;
}
</style>